home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / asm / games / texturemapping / demomain.asm < prev    next >
Assembly Source File  |  1980-01-03  |  11KB  |  419 lines

  1. ; $$TABS=8
  2. ;
  3. ; GameDemo - texture mapping and Gouraud shading Demo by Chris Green
  4. ;
  5. ; This code is both AA specific and (in some parts) V39 specific.
  6. ;
  7. ; NOTE: This code is in no way optimized. I only spent about 1 week on
  8. ; it total.
  9. ;
  10.  
  11.     include    'exec/types.i'            ; get structure definition macros, etc.
  12.     include    'exec/memory.i'            ; get memory type flags
  13.     include    'exec/interrupts.i'        ; get interrupt structure
  14.  
  15.      include    'intuition/intuition.i'        ; get window defs' etc.
  16.     include    'intuition/screens.i'        ; get screen attributes, etc.
  17.     include    'graphics/displayinfo.i'    ; get display-id definitions
  18.     include    'graphics/view.i'        ; display modes
  19.     include    'hardware/custom.i'        ; get register offsets
  20.     include    'devices/timer.i'        ; get timer.device stuff
  21.  
  22.     include    'demo.i'            ; master include file for this program.
  23.  
  24.     xref    _LVOOpenLibrary,_LVOCloseLibrary,_LVOOpenDevice,_LVOCloseDevice
  25.     xref    _LVOOpenScreenTagList,_LVOCloseScreen,_LVODelay
  26.     xref    _LVOAllocMem,_LVOUCopperListInit,_LVORemakeDisplay,_LVOFreeMem
  27.     xref    _LVOScreenToFront,_LVOAddIntServer,_LVORemIntServer
  28.     xref    _LVOSignal,_LVOAllocSignal,_LVOWait,_LVORemTask
  29.     xref    _LVOObtainSemaphore,_LVOReleaseSemaphore
  30.     xref    _LVOScrollVPort,_LVOForbid,_LVOPermit,_LVOLoadRGB32
  31.     xref    _LVOSetSignal,_LVOAllocBitMap,_LVOFreeBitMap
  32.     xref    _LVOSetAPen,_LVOMove,_LVODraw,_LVOInitRastPort,_LVOSetRast
  33.     xref    _LVOReadEClock,_LVOGetBitMapAttr
  34.  
  35.     xref    _AbsExecBase            ; it's 4, but let's use the symbol anyway.
  36.     xref    _custom
  37.     xref    _CreateTask
  38.     xref    ReadRaw
  39.     xref    init_fakefb,fill_fakefb,fakefb_to_screen,fill_fakefb_gradient
  40.     xref    YLRFill,UnClippedPolygon,vertex,current_color
  41.     xref    DoMovement,PlayerHeading,PlayerX,PlayerY,PlayerZ,PlayerSpeed
  42.     xref    stick_click
  43.     xref    render_3d
  44.  
  45. main::
  46.     ONTIMER    0
  47.     geta6                ; in this program, a6 is used for most variable references,
  48.                     ; to save space and speed.
  49.  
  50.     move.l    a7,InitialSP(a6)
  51.     move.l    _AbsExecBase,_SysBase(a6)    ; let's use a local copy so that it might be in fast ram
  52.  
  53.     openlib    intuiname(pc),_IntuitionBase
  54.     openlib    gfxname(pc),_GfxBase
  55.     openlib    dosname(pc),_DosBase
  56.  
  57. ; now, we must allocate a bitmap for the main scrolling area.
  58.     move.l    _GfxBase(a6),a6
  59.     move.l    #CANVAS_WIDTH,d0
  60.     move.l    #CANVAS_HEIGHT,d1
  61.     move.l    #SCREEN_DEPTH,d2
  62.     move.l    #BMF_DISPLAYABLE|BMF_CLEAR|BMF_INTERLEAVED,d3
  63.     sub.l    a0,a0
  64.     jsr    _LVOAllocBitMap(a6)
  65.     geta6
  66.     move.l    d0,bm_tag+4
  67.     move.l    d0,canvas_bitmap(a6)
  68.     beq    quiet_error
  69. ; now, initialize a rastport for drawing into the canvas bitmap
  70.     move.l    a6,a5                ; keep varbase temporarily in a5
  71.     move.l    _GfxBase(a6),a6
  72.     move.l    d0,a0        ; a0=bitmap
  73.     move.l    #BMA_FLAGS,d1    ; get bitmap flags
  74.     jsr    _LVOGetBitMapAttr(a6)    ; test interleaved state
  75.     btst    #BMB_INTERLEAVED,d0    ; is interleaved?
  76.     beq    quiet_error    ; bomb out if not interleaved
  77.  
  78.     lea    canvas_rport(a5),a1
  79.     jsr    _LVOInitRastPort(a6)
  80.     move.l    canvas_bitmap(a5),canvas_rport+rp_BitMap(a5)
  81.     
  82. ; all libraries are open, now let's open our demo screen.
  83.     sub.l    a0,a0            ; no NewScreen structure, just tags
  84.     lea    screentaglist(pc),a1    ; screen attributes
  85.     move.l    _IntuitionBase(a5),a6
  86.     jsr    _LVOOpenScreenTagList(a6)
  87.     geta6
  88.     move.l    d0,DemoScreen(a6)
  89.     beq    quiet_error
  90.     
  91. ; now, we must allocate a UCopList structure for our user copper list
  92.  
  93. ;    move.l    #ucl_SIZEOF,d0
  94. ;    move.l    #MEMF_PUBLIC|MEMF_CLEAR,d1    ; as per autodoc for CINIT
  95. ;    move.l    _SysBase(a6),a6
  96. ;    jsr    _LVOAllocMem(a6)
  97. ;    geta6
  98. ;    move.l    d0,UserCopList(a6)
  99. ;    beq    quiet_error
  100.     
  101. ;    move.l    d0,a0
  102. ;    move.l    d0,a2                ; save value
  103. ;    move.l    #30,d0                ; room for 30 copper instructions
  104. ;    move.l    _GfxBase(a6),a6
  105. ;    jsr    _LVOUCopperListInit(a6)
  106.  
  107. ;    CWAIT    a2,#0,#0            ; wait for first line
  108. ;    CMOVE    a2,#intreq,#$8010        ; cause an interrupt
  109. ;    CEND    a2
  110. ;    geta6
  111.  
  112. ; now, let's install the user copperlist into our screen's viewport.
  113.  
  114.     move.l    DemoScreen(a6),a0
  115. ;    move.l    a2,sc_ViewPort+vp_UCopIns(a0)
  116.  
  117.     move.l    DemoScreen(a6),a0
  118.     lea    sc_ViewPort(a0),a1
  119.     move.l    a1,canvas_viewport(a6)
  120.  
  121.     lea    timername(pc),a0        ; device name
  122.     moveq    #0,d0                ; unit #
  123.     lea    TimerIO(a6),a1            ; iorequest
  124.     moveq    #0,d1                ; flags
  125.     move.l    $4,a6
  126.     jsr    _LVOOpenDevice(a6)
  127.     geta6
  128.     tst.l    d0                ; error?
  129.     bne    quiet_error
  130.  
  131.  
  132.  
  133.     move.l    canvas_viewport(a6),a0
  134.     lea    mytab(pc),a1
  135.     move.l    _GfxBase(a6),a6
  136.     jsr    _LVOLoadRGB32(a6)
  137.     geta6
  138.  
  139.  
  140. ; now, let's create the copper-triggered task
  141. ;    pea    $1000            ; stack size
  142. ;    pea    realtime_task(pc)    ; code ptr
  143. ;    pea    25            ; priority
  144. ;    pea    mytaskname(pc)        ; name
  145. ;    jsr    _CreateTask        ; call amiga.lib entry point
  146. ;    lea    4*4(a7),a7        ; pop stacked args
  147. ;    move.l    d0,thetask(a6)
  148. ;    move.l    d0,CopperIntr+IS_DATA(a6)
  149.  
  150.  
  151.  
  152. ; now, let's set up the copper interrupt handler.
  153. ;    move.w    #$8010,_custom+intena        ; enable copper ints
  154. ;    move.b    #10,LN_PRI+CopperIntr(a6)    ; pick a nice priority
  155. ;    move.l    #myintr_handler,IS_CODE+CopperIntr(a6)
  156. ;    lea    CopperIntr(a6),a1
  157. ;    move.l    _SysBase(a6),a6
  158. ;    moveq    #4,d0                ; interrupt number
  159. ;    jsr    _LVOAddIntServer(a6)
  160. ;    geta6
  161. ;    st    server_added(a6)        ; flag for whether server was added
  162.     bsr    init_fakefb
  163.  
  164. main_loop:    
  165.     moveq    #0,d0
  166.     bsr    fill_fakefb
  167.     move.l    d7,-(a7)
  168.     bsr    DoMovement
  169.     bsr    render_3d
  170.     bsr    get_elapsed_time
  171.     move.w    d0,frfract(a6)
  172.     ifne    DO_DIAGNOSTICS
  173.     ONTIMER    2
  174.     move.w    frfract(a6),d0
  175.     beq.s    no_frate
  176.     move.l    #$8000*10,d1
  177.     divu    d0,d1
  178.     moveq    #0,d0
  179.     move.w    d1,d0
  180.     dstring    'Frame rate=%04ld',DUNGEON_WINDOW_RIGHT+10,DUNGEON_WINDOW_Y+20,d0
  181. no_frate:
  182.     ifne    0
  183.     moveq    #0,d0
  184.     move.w    PlayerHeading(a6),d0
  185.     dstring    'Head=%04lx',DUNGEON_WINDOW_RIGHT+5,DUNGEON_WINDOW_Y+30,d0
  186.     moveq    #0,d0
  187.     move.w    PlayerSpeed(a6),d0
  188.     dstring    'v=%04lx',DUNGEON_WINDOW_RIGHT+5,DUNGEON_WINDOW_Y+40,d0
  189.     dstring    'X=%7ld',DUNGEON_WINDOW_RIGHT+5,DUNGEON_WINDOW_Y+50,PlayerX(a6)
  190.     dstring    'Y=%7ld',DUNGEON_WINDOW_RIGHT+5,DUNGEON_WINDOW_Y+60,PlayerY(a6)
  191.     dstring    'Z=%7ld',DUNGEON_WINDOW_RIGHT+5,DUNGEON_WINDOW_Y+70,PlayerZ(a6)
  192.     endc
  193.     OFFTIMER    2
  194.     endc
  195.     ONTIMER    1
  196.     bsr    fakefb_to_screen
  197.     OFFTIMER    1
  198.     move.l    (a7)+,d7
  199.     tst.b    stick_click(a6)
  200.     beq    main_loop
  201.     
  202. quit_demo:
  203.     bsr    cleanup_resources        ; close everything
  204.     moveq    #0,d0                ; return code
  205.     rts                    ; and return to shell
  206.  
  207. temp_ylr:
  208.     dc.w    0,0,1,-1
  209.  
  210. myintr_handler:
  211.     move.l    canvas_viewport,a6
  212.     move.w    vp_Modes(a6),d0
  213.     and.w    #V_VP_HIDE,d0
  214.     bne.s    no_signal
  215.     move.l    $4.w,a6
  216.     move.l    CycleTaskSignal,d0
  217.     beq.s    no_signal
  218.     jsr    _LVOSignal(a6)
  219. no_signal:
  220.     moveq    #0,d0            ; let others run
  221.     rts
  222.  
  223.  
  224. realtime_task:
  225. ; entry point for beam-triggered display task.
  226.     geta6
  227.     move.l    _SysBase(a6),a6
  228.     moveq    #-1,d0
  229.     jsr    _LVOAllocSignal(a6)
  230.     geta6
  231.     moveq    #1,d1
  232.     lsl.l    d0,d1
  233.     move.l    d1,d0
  234.     move.l    d0,CycleTaskSignal(a6)
  235.     move.l    d0,d7
  236.     move.l    a6,a5
  237. CycleTaskLoop:
  238.     move.l    _SysBase(a5),a6
  239.     move.l    d7,d1
  240.     moveq    #0,d0
  241.     jsr    _LVOSetSignal(a6)
  242.     move.l    d7,d0
  243.     jsr    _LVOWait(a6)
  244.     ifd    SCROLL_TEST
  245.     move.l    DemoScreen(a5),a0
  246.     lea    sc_ViewPort(a0),a0
  247.     st    not_safe(a5)        ; tell the main task that
  248.     move.l    vp_RasInfo(a0),a1
  249.  
  250.     addq.w    #1,ri_RyOffset(a1)
  251.     cmp.w    #200,ri_RyOffset(a1)
  252.     bls.s    1$
  253.     clr.w    ri_RyOffset(a1)
  254. 1$:
  255.     addq.w    #1,ri_RxOffset(a1)
  256.     cmp.w    #320,ri_RxOffset(a1)
  257.     bls.s    2$
  258.     clr.w    ri_RxOffset(a1)
  259. 2$:    move.l    _GfxBase(a5),a6        ; it is not safe to RemTask me.
  260.     jsr    _LVOScrollVPort(a6)
  261.     move.w    #$f0f,$dff180
  262.     sf    not_safe(a5)
  263.     endc
  264.     bra    CycleTaskLoop
  265.  
  266. mytab:
  267.     include    'palette.i'        ; use default palette
  268.  
  269. quiet_error::
  270. ; entr: none. exit: doesn't. trashes: all
  271. ; this routine exits from the program, cleaning up any resources, but with no error
  272. ; message. This is because some library may have failed to open.
  273.     geta6                ; this routine can be entered without a6 pointing at variable base.
  274.     bsr    cleanup_resources    ; close libraries
  275.     move.l    InitialSP(a6),a7
  276.     moveq    #30,d0            ; return code
  277.     rts
  278.  
  279. cleanup_resources:
  280. ; close all screens, libraries, free memory, etc.
  281.     OFFTIMER    0
  282.     tst.l    TimerIO+IO_DEVICE(a6)    ; timer open ?
  283.     beq.s    no_timer
  284.     lea    TimerIO(a6),a1
  285.     move.l    $4,a6
  286.     jsr    _LVOCloseDevice(a6)
  287.     geta6
  288. no_timer:
  289.     tst.b    server_added(a6)
  290.     beq.s    no_server
  291.     lea    CopperIntr(a6),a1
  292.     moveq    #4,d0            ; copper intr
  293.     move.l    _SysBase(a6),a6
  294.     jsr    _LVORemIntServer(a6)
  295.     geta6
  296.     clr.b    server_added(a6)
  297. no_server:
  298.     move.l    thetask(a6),a1
  299.     cmp.w    #0,a1
  300.     beq.s    notask
  301.     move.l    _SysBase(a6),a6
  302. busy_loop:
  303.     jsr    _LVOForbid(a6)
  304.     tst.b    not_safe
  305.     beq.s    is_safe
  306.     jsr    _LVOPermit(a6)
  307.     bra.s    busy_loop
  308. is_safe:
  309.     jsr    _LVORemTask(a6)
  310.     jsr    _LVOPermit(a6)
  311.     geta6
  312. notask:
  313.     move.l    DemoScreen(a6),d0
  314.     beq.s    screen_not_open
  315.     move.l    d0,a0
  316.     move.l    _IntuitionBase(a6),a6
  317.     jsr    _LVOCloseScreen(a6)
  318.     geta6
  319.     clr.l    DemoScreen(a6)
  320. screen_not_open:
  321.     move.l    canvas_bitmap(a6),a0
  322.     move.l    _GfxBase(a6),a6
  323.     jsr    _LVOFreeBitMap(a6)    ; FreeBitMap(NULL) is ok
  324.     geta6
  325.     closelib    _IntuitionBase
  326.     closelib    _GfxBase
  327.     closelib    _DosBase
  328.     rts
  329.  
  330.  
  331. get_elapsed_time::
  332. ; return d0=elapsed time in int.frac format
  333. ; trashes: a0/a1/d1-d4
  334.     lea    NewEClock(a6),a0
  335.     move.l    TimerIO+IO_DEVICE(a6),a6    ; get library pointer
  336.     jsr    _LVOReadEClock(a6)        ; now, NewEClock=64 bit value
  337.     geta6
  338.     movem.l    LastEClock(a6),d1/d2/d3/d4    ; d1/d2=old d3/d4=new
  339.     movem.l    d3/d4,LastEClock(a6)
  340.     tst.b    first_timer(a6)
  341.     beq.s    second_time
  342.     clr.b    first_timer(a6)
  343.     moveq    #0,d0
  344.     rts
  345. second_time:
  346.     sub.l    d2,d4
  347.     subx.l    d1,d3                ; d3 now=elapsed time
  348.     add.l    d0,d0
  349.     clr.w    d0
  350.     swap    d0            ; d0=#ticks in (1/32768s)
  351.     divu.l    d0,d3:d4
  352.     move.l    d4,d0
  353.     rts
  354.  
  355.  
  356. screentaglist:
  357. ; list of attributes for the screen that we want to open
  358.     dc.l    SA_Width,SCREEN_WIDTH,SA_Height,SCREEN_HEIGHT,SA_Depth,SCREEN_DEPTH
  359.     dc.l    SA_Quiet,-1                ; prevent gadgets, titlebar from appearing.
  360.     dc.l    SA_DisplayID,0                ; default (can be promoted)
  361. bm_tag::
  362.     dc.l    SA_BitMap,0
  363.     dc.l    TAG_END
  364.  
  365.  
  366.  
  367. dosname:
  368.     dc.b    'dos.library',0
  369. intuiname:
  370.     dc.b    'intuition.library',0
  371. gfxname:
  372.     dc.b    'graphics.library',0
  373.  
  374. mytaskname:
  375.     dc.b    'Demo Color Cycle Task',0
  376.  
  377. timername:
  378.     dc.b    'timer.device',0
  379.  
  380.     section    __MERGED,DATA
  381.  
  382. InitialSP::    dc.l    0        ; initial stack pointer so that we can return to the
  383.                     ; shell from any stack depth
  384. _SysBase::    dc.l    0        ; my copy of ExecBase
  385. _IntuitionBase::
  386.         dc.l    0        ; Intuition library base ptr
  387. _GfxBase::    dc.l    0        ; graphics library ptr
  388. _DosBase::    dc.l    0        ; dos library ptr
  389.  
  390. DemoScreen::    dc.l    0        ; pointer to our screen
  391.  
  392. UserCopList::    dc.l    0        ; pointer to our allocated user copper list
  393.  
  394. thetask::    dc.l    0
  395. CycleTaskSignal::
  396.         dc.l    0
  397.  
  398. canvas_bitmap::    dc.l    0        ; bitmap for scrolling canvas
  399. canvas_viewport::
  400.         dc.l    0        ; viewport of canvas screen
  401.  
  402. CopperIntr::    ds.b    IS_SIZE        ; interrupt structure for my copper interrupt
  403.  
  404. frfract::    dc.w    0        ; elapsed time 32767=1 sec
  405. TimerIO::    ds.b    IOTV_SIZE
  406.  
  407. ; the following two must stay together
  408. LastEClock::    dc.l    0,0        
  409. NewEClock::    dc.l    0,0
  410. ; the preceeding two must stay together
  411.  
  412. canvas_rport::    ds.b    rp_SIZEOF    ; rastport for writing into canvas bitmap
  413. server_added::    dc.b    0
  414. not_safe::    dc.b    0
  415. first_timer::    dc.b    -1        ; set if get_elapsed_time hasn't been called yet
  416. ctdn::    dc.b    0
  417.  
  418.     end
  419.